/*******************************************************************************
 * Copyright (c) 2008 Smart Mai(smardio@gmail.com).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Smart Mai(smardio@gmail.com) - initial API and implementation
 ******************************************************************************/
package com.smartdio.company.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.ui.handlers.HandlerUtil;

import com.smartdio.company.model.Company;
import com.smartdio.company.model.CompanyModel;
import com.smartdio.company.views.CompanyEditDialog;
import com.smartdio.company.views.CompanyEditPanel;
import com.smartdio.company.views.CompanyView;

/**
 * @author smart
 * 
 */
public class AddCompany extends AbstractHandler implements IHandler {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
	 * ExecutionEvent)
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		CompanyEditDialog dialog = new CompanyEditDialog(HandlerUtil
				.getActiveShell(event));
		
		// 设置对话框显示的方式为模式对话框，只有对话框关闭后才能执行其他操作。
		dialog.setBlockOnOpen(true);
		
		// 打开对话框，并评断对话框关闭后的返回，如果不是OK，则什么也不做
		if (!(dialog.open() == dialog.OK))
			return null;
		// 获得对话框中输入的内容
		CompanyEditPanel panel = dialog.editPanel;
		// 构造一个Company，把对话框输入的内容设置到Company中
		Company company = new Company(true);
		company.name = panel.getName();
		company.description = panel.getDescription();
		// 添加新的Company实体记录
		CompanyModel.getInstance().addComapny(company);
		// 更新CompanyView
		if (CompanyView.instance != null) {
			CompanyView.instance.refresh();
		}
		return null;
	}

}
